package com.pandai.leetcode;

public class Solution24 {
    public ListNode swapPairs(ListNode head) {
        ListNode dummyNode = new ListNode(0);
        dummyNode.next = head;
        ListNode cur = dummyNode;
        // 循环条件表示每次处理两个结点，当只有一个结点或无结点处理时，循环结束
        while(cur.next!=null && cur.next.next!=null){
            // cur指向两个即将交换节点的前一个结点
            // 例如要处理1和2两个节点，cur就指向1前面的一个结点
            ListNode first = cur.next;
            ListNode second = first.next;
            ListNode tmp = second.next;
            cur.next = second;
            second.next = first;
            first.next = tmp;
            cur = first;
        }
        return dummyNode.next;
    }

    public static void main(String[] args) {
        Solution24 solution = new Solution24();
        ListNode head = new ListNode(1);
        head.next = new ListNode(2);
        head.next.next = new ListNode(3);
        head.next.next.next = new ListNode(4);
        solution.swapPairs(head);
    }
}
